package servlet;

import user.Book;
import util.JDBC;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class EditServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ISBN = (String) request.getSession().getAttribute("ISBN");
        String name = request.getParameter("name");
        String author = request.getParameter("author");
        String press = request.getParameter("press");
        Book book = new Book(ISBN, name, author, press);

        ConnectServlet conn = new ConnectServlet();
        conn.doGet(request, response);
        Connection connection = conn.getConnection(); //获取连接
        try {
            JDBC.update(book, connection); //更新
            List<Book> list = JDBC.getBook(connection); //获取所有图书
            request.setAttribute("book", list);
            connection.close();
            JDBC.release();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        request.getRequestDispatcher("result.jsp").forward(request, response);
    }
}
